﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cl.Utils.Helper.Compare
{
    public class CompareHelper
    {
        public static string RecordDifferences<T>(T before, T after)
        {
            string result = string.Empty;

            var properties = typeof(T).GetProperties();

            foreach (var prop in properties)
            {

                string strBefore = prop?.GetValue(before)?.ToString() ?? string.Empty;
                string strAfter = prop?.GetValue(after)?.ToString() ?? string.Empty;

                if (strBefore != strAfter)
                {
                    // 属性名
                    var propName = prop.Name;

                    if (propName != "UpdateTime")
                    {
                        // 旧值
                        var beforeValue = prop.GetValue(before)?.ToString();
                        // 新值
                        var afterValue = prop.GetValue(after)?.ToString();

                        // 记录不同属性的名称和值
                        result += $"属性[{propName}]从[{beforeValue}]更改为[{afterValue}];";
                        //Console.WriteLine($"Property changed: {propName}");
                        //Console.WriteLine($"Before: {beforeValue}");
                        //Console.WriteLine($"After: {afterValue}");
                    }
                }


            }

            return result;
        }
    }
}
